<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateGoodsEvaluateRequest;
use App\Http\Requests\UpdateGoodsEvaluateRequest;
use App\Models\GoodsEvaluate;
use App\Repositories\GoodsEvaluateRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Collection;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class GoodsEvaluateController extends AppBaseController
{
    /** @var  GoodsEvaluateRepository */
    private $goodsEvaluateRepository;

    public function __construct(GoodsEvaluateRepository $goodsEvaluateRepo)
    {
        $this->goodsEvaluateRepository = $goodsEvaluateRepo;
    }

    /**
     * Display a listing of the GoodsEvaluate.
     *
     * @param Request $request
     * @return Response
     */
//    public function index(){
//        return view('goods_evaluates.index');
//    }
    public function index(Request $request)
    {
        $this->goodsEvaluateRepository->pushCriteria(new RequestCriteria($request));

        $session = session();
        if($request->seesion_clear){
            $session->forget('search_page');
        }
        $seesion_search = $request->seesion_search;
        if(session('search_page') && $seesion_search)
            $request->merge(['page'=>session('search_page')]);
        if($request->page){
            session(['search_page'=>$request->page]);
        }
        $goodsEvaluates = GoodsEvaluate::orderBy('id','desc')->paginate(20);
        $goodsEvaluates->load('goods');
        $goodsEvaluates->load('attribute');
        $goodsEvaluates->load('user');


//        foreach ($goodsEvaluates as &$goodsEvaluate) {
//            $pics = json_decode($goodsEvaluate->pictures,true);
//            $goodsEvaluate->pictures = $pics?collect(($pics)):[];
//        }
        return view('goods_evaluates.index')
            ->with('goodsEvaluates', $goodsEvaluates)->with('request',$request->all())->with('request',array_merge($request->all()));
    }

    /**
     * Show the form for creating a new GoodsEvaluate.
     *
     * @return Response
     */
    public function create()
    {
        return view('goods_evaluates.create');
    }

    /**
     * Store a newly created GoodsEvaluate in storage.
     *
     * @param CreateGoodsEvaluateRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsEvaluateRequest $request)
    {
        $input = $request->all();

        $goodsEvaluate = $this->goodsEvaluateRepository->create($input);

        Flash::success('Goods Evaluate saved successfully.');

        return redirect(route('goodsEvaluates.index'));
    }

    /**
     * Display the specified GoodsEvaluate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $goodsEvaluate = $this->goodsEvaluateRepository->findWithoutFail($id);

        if (empty($goodsEvaluate)) {
            Flash::error('Goods Evaluate not found');

            return redirect(route('goodsEvaluates.index'));
        }

        return view('goods_evaluates.show')->with('goodsEvaluate', $goodsEvaluate);
    }

    /**
     * Show the form for editing the specified GoodsEvaluate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $goodsEvaluate = $this->goodsEvaluateRepository->findWithoutFail($id);

        if (empty($goodsEvaluate)) {
            Flash::error('Goods Evaluate not found');

            return redirect(route('goodsEvaluates.index'));
        }

        return view('goods_evaluates.edit')->with('goodsEvaluate', $goodsEvaluate);
    }

    /**
     * Update the specified GoodsEvaluate in storage.
     *
     * @param  int              $id
     * @param UpdateGoodsEvaluateRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsEvaluateRequest $request)
    {
        $goodsEvaluate = $this->goodsEvaluateRepository->findWithoutFail($id);

        if (empty($goodsEvaluate)) {
            Flash::error('Goods Evaluate not found');

            return redirect(route('goodsEvaluates.index'));
        }

        $goodsEvaluate = $this->goodsEvaluateRepository->update($request->all(), $id);

        Flash::success('Goods Evaluate updated successfully.');

        return redirect(route('goodsEvaluates.index',['seesion_search'=>1]));
    }

    /**
     * Remove the specified GoodsEvaluate from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $goodsEvaluate = $this->goodsEvaluateRepository->findWithoutFail($id);

        if (empty($goodsEvaluate)) {
            Flash::error('Goods Evaluate not found');

            return redirect(route('goodsEvaluates.index'));
        }

        $this->goodsEvaluateRepository->delete($id);

        Flash::success('Goods Evaluate deleted successfully.');

        return redirect(route('goodsEvaluates.index',['seesion_search'=>1]));
    }

    public function reply(Request $request)
    {
        GoodsEvaluate::where('id', $request->reply_id)->update(['reply'=>$request->reply_content]);
        Flash::success('回复成功.');
        return redirect(route('goodsEvaluates.index',['seesion_search'=>1]));
    }
}
